﻿using System;

namespace Microsoft.Popfly.GameCreator.GameEngine.Events
{
    public abstract class EventListener
    {
        public string BehaviorName { get; private set; }
        public string AudioUrl { get; private set; }
        public BehaviorOwner Owner { get; private set; }
        public Filter Filter { get; private set; }
        public Behaviors.IBehavior Behavior { get; private set; }
        public bool QueuedForRemoval = false;

        protected Game m_game;

        public EventListener(Game game, BehaviorOwner owner, Filter filter, Behaviors.IBehavior behavior, string behaviorName, string audioUrl)
        {
            m_game = game;
            Owner = owner;
            Filter = filter;
            Behavior = behavior;
            BehaviorName = behaviorName;
            AudioUrl = audioUrl;
        }

        public EventListener(Game game, BehaviorOwner owner, Data.BehaviorInfo behaviorInfo)
        {
            m_game = game;
            Owner = owner;
            BehaviorName = behaviorInfo.Name;

            if (behaviorInfo.Event.Filter != null)
            {
                Filter = new Filter(m_game, owner, behaviorInfo.Event.Filter);
            }
            else
            {
                Filter = null;
            }

            if (behaviorInfo.BehaviorType == "sound")
            {
                Behavior = new Behaviors.SoundBehavior(game, behaviorInfo.Audio.Url);
            }
            else
            {
                if (behaviorInfo.Audio != null)
                {
                    AudioUrl = behaviorInfo.Audio.Url;
                }
                Behavior = Behaviors.BehaviorFactory.CreateBehavior(game, owner, behaviorInfo);
#if PHONE
                if (Behavior == null) { Behavior = new Behaviors.SoundBehavior(game, behaviorInfo.Audio.Url); }
#endif
            }
        }
    }
}
